// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_PUR_PO
// * CreateDate    : 2014-02-18 18:04:10
// * Creater       : CodeSmith
// * LastChangeDate: 2014-02-18 18:04:10
// * LastChanger   : 2014-02-18 18:04:10
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Pur
{
/// <summary>
    /// PurPo
    /// </summary>
    public partial class PurPoBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objPurPoEntity">Entity</param>
        /// <returns>Entity</returns>
        public static PurPoEntity GetPurPo(PurPoEntity objPurPoEntity)
        {
            return DataAccess.SelectSingle<PurPoEntity>(objPurPoEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objPurPoEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertPurPo(PurPoEntity objPurPoEntity)
        {
            return DataAccess.Insert(objPurPoEntity);
        }

        public static bool InsertPurPo(List<PurPoEntity> objPurPoEntityList)
        {
            return DataAccess.Insert(objPurPoEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objPurPoEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdatePurPo(PurPoEntity objPurPoEntity)
        {
            return DataAccess.Update(objPurPoEntity);
        }

        public static bool UpdatePurPo(List<PurPoEntity> objPurPoEntityList)
        {
            return DataAccess.Update<PurPoEntity>(objPurPoEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objPurPoEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeletePurPo(PurPoEntity objPurPoEntity)
        {
            return DataAccess.Delete(objPurPoEntity);
        }

        public static bool DeletePurPo(List<PurPoEntity> objPurPoEntityList)
        {
            return DataAccess.Delete<PurPoEntity>(objPurPoEntityList);
        }

        public static bool DeletePurPo(PurPoEntity objPurPoEntity,List<PurPoItemEntity> items)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (items != null)
                        DataAccess.Delete(items, broker);
                    DataAccess.Delete(objPurPoEntity, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    return false;
                }
            }
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetPurPoAll()
        {
            return DataAccess.SelectAllDataSet<PurPoEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<PurPoEntity> GetPurPoAllList()
        {
            return DataAccess.SelectAll<PurPoEntity>();
        }
        #endregion

        public static bool AddPurPo(PurPoEntity po, List<PurPoItemEntity> items)
        {
            if (po == null)
                return false;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccess.Insert(po, broker);
                    if (items != null)
                        DataAccess.Insert(items, broker);

                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    return false;
                }
            }

        }
        
        public static DataTable GetPOArvAndStockInDetail()
        {
            string sql = @" SELECT I.PO_NO,I.MATERIAL_ID,I.MATERIAL_CODE MTRL_NO,I.MATERIAL_NAME MTRL_NAME,I.QTY AS PO_QTY
 ,ISNULL(ARV.ARV_QTY,0) ARV_QTY 
 ,ISNULL(ARV.STOCK_IN_QTY,0) STOCK_IN_QTY 
 ,(I.QTY-ISNULL(ARV.STOCK_IN_QTY,0)) RECEIVING_QTY
 FROM dbo.PUR_PO_ITEM I LEFT JOIN dbo.PUR_PO PO ON I.PO_NO=PO.PO_NO
LEFT JOIN 
(SELECT B.PO_NO, A.MATERIAL_ID,SUM(A.QTY) ARV_QTY,SUM(A.STOCK_IN_QTY) STOCK_IN_QTY  FROM dbo.PUR_ARV_ITEM A  LEFT JOIN dbo.PUR_ARV B ON A.ARV_NO=B.ARV_NO 
GROUP BY B.PO_NO, A.MATERIAL_ID)
ARV ON I.MATERIAL_ID=ARV.MATERIAL_ID AND I.PO_NO=ARV.PO_NO

";
            //            LEFT JOIN 
            //{SELECT SUM(QTY) INV_QTY ,IOI.PRODUCT_ID,IO.RELATED_ID,IO.IO_NO FROM INV_IOI IOI LEFT JOIN INV_IO IO ON IOI.IO_ID=IO.ID
            //WHERE IO.STATUS='WFS30'
            //GROUP BY IOI.PRODUCT_ID,IO.RELATED_ID,IO.IO_NO) INV
            //ON INV.RELATED_ID=I.PO_NO AND INV.PRODUCT_ID=I.MATERIAL_ID

            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
                return ds.Tables[0];
            else
                return null;


        }
        
        public static DataTable GetPRNotToPo()
        {
            string sql = @" SELECT pr.APP_NO,i.PRODUCT_ID,I.PRODUCT_NAME MTRL_NAME,I.PRODUCT_CODE MTRL_NO,ISNULL(I.QTY,0) AS PRED_QTY FROM dbo.PUR_APP_ITEM I LEFT JOIN dbo.PUR_APP PR ON I.PUR_APP_ID=PR.ID
 where I.STATUS='PUR_ITEM10' AND PR.STATUS!='WFS40'
";
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
                return ds.Tables[0];
            else
                return null;


        }
	}
	
	
}	

